package com.kaibes.platform.file.client.token;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaibes.core.base.util.AesUtils;
import com.kaibes.platform.file.client.db.FileMap;
import com.kaibes.platform.file.client.db.FileServer;

@Component
public class FileTokenUtils {

    @Autowired
    private ObjectMapper objectMapper;
    
    public String writeFileString(FileServer fileServer, FileString fileString) {
        try {
            String fileStr = objectMapper.writeValueAsString(fileString);
            fileStr = AesUtils.CBC.encrypt2String64(fileStr, fileServer.getAesKey(), fileServer.getAesIv());
            return fileStr;
        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException
                | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public FileMap readFileMap(FileServer fileServer, String fileStr) {
        try {
            fileStr = AesUtils.CBC.decrypt2String64(fileStr, fileServer.getAesKey(), fileServer.getAesIv());
            return objectMapper.readValue(fileStr, FileMap.class);
        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException
                | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    public FileString readFileString(FileServer fileServer, String fileStr) {
        try {
            fileStr = AesUtils.CBC.decrypt2String64(fileStr, fileServer.getAesKey(), fileServer.getAesIv());
            return objectMapper.readValue(fileStr, FileString.class);
        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException
                | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public String writeListString(FileServer fileServer, List<String> listString) {
        try {
            String fileStr = objectMapper.writeValueAsString(listString);
            fileStr = AesUtils.CBC.encrypt2String64(fileStr, fileServer.getAesKey(), fileServer.getAesIv());
            return fileStr;
        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException
                | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
}
